package com.bicon.botu.account.handler;

import com.alibaba.fastjson.JSONObject;
import com.bicon.botu.account.common.AccountConstants;
import com.bicon.botu.account.dto.RoleDto;
import com.bicon.botu.account.entity.AccountInfo;
import com.bicon.botu.account.mapper.AccountRoleMapper;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.utils.DateUtil;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author miou
 * Created by fyx on 2017/3/14 0014.
 */
@Component
public class AccountHandler {

    @Autowired
    private RedisHandler redisHandler;

    @Autowired
    private AccountRoleMapper accountRoleMapper;

    /**
     * 用户登录时，保存token
     *
     * @param account
     * @param token
     * @param userId
     * @return
     */
    public boolean initToken(String account, String token, String userId, String uuId) {
        boolean result = false;
        //移除原先缓存
        String oldToken = redisHandler.getGlobalRedisTemplate().getStringValue(AccountConstants.BOTU_USER_TOKEN, account);
        if (StringUtils.isNotBlank(oldToken)) {
            //删除缓存处理
            redisHandler.getGlobalRedisTemplate().remove(AccountConstants.BOTU_USER_TOKEN, account);
            redisHandler.getGlobalRedisTemplate().remove(AccountConstants.BOTU_USER_TOKEN, oldToken);
            //记录登录日志
        }
        //添加新缓存
        if (
                redisHandler.getGlobalRedisTemplate().cacheStringValue(AccountConstants.BOTU_USER_TOKEN, account, token, AccountConstants.TOKEN_EXPIRE_TIME) &&
                        redisHandler.getGlobalRedisTemplate().cacheStringValue(AccountConstants.BOTU_USER_TOKEN, token, userId, AccountConstants.TOKEN_EXPIRE_TIME)) {
            redisHandler.getGlobalRedisTemplate().cacheHash(AccountConstants.BOTU_USER_LOGIN_LOG, account, token + ":" + oldToken, String.format(AccountConstants.BOTU_USER_LOGIN_LOG_DES, account, DateUtil.convert(new Date()), token, oldToken), AccountConstants.TOKEN_EXPIRE_TIME);
            result = true;
        }
        return result;
    }

    /**
     * 用户登录时，缓存用户角色到redis
     *
     * @param accountId 账户id
     * @return
     */
    public void initAccountRole(String accountId) {
        redisHandler.getGlobalRedisTemplate().remove(AccountConstants.ACCOUNT_ROLE, accountId);
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("type", "0");
        map.put("logicDelete", Constants.DEL_LOGIC_NORMAL);
        List<RoleDto> roles = accountRoleMapper.getRoleByAccountId(map);
        if (!roles.isEmpty() && roles.size() > 0) {
            List<String> list = new ArrayList<>();
            for (RoleDto role : roles) {
                list.add(role.getId());
            }
            redisHandler.getGlobalRedisTemplate().cacheList(AccountConstants.ACCOUNT_ROLE, accountId, list, AccountConstants.TOKEN_EXPIRE_TIME);
        }

    }
    /**
     * 缓存移动端用户
     * @param account
     */
    public void saveAccount(AccountInfo account) {
        String accountStr = redisHandler.getGlobalRedisTemplate().getStringValue(Constants.BUTO_USER_ACCOUNT, account.getId());
        if (null != accountStr) {
            redisHandler.getGlobalRedisTemplate().remove(Constants.BUTO_USER_ACCOUNT, account.getId());
        }
        account.setPassword("");
        redisHandler.getGlobalRedisTemplate().cacheStringValue(Constants.BUTO_USER_ACCOUNT, account.getId(), JSONObject.toJSON(account).toString());
    }

}
